package com.zhonghui.wms.service.impl;

import com.zhonghui.common.utils.SecurityUtils;
import com.zhonghui.common.utils.StringUtils;
import com.zhonghui.wms.domain.WmsCarrierApplication;
import com.zhonghui.wms.domain.WmsCarrierApplicationDetails;
import com.zhonghui.wms.domain.vo.WmsCarrierApplicationVo;
import com.zhonghui.wms.mapper.WmsCarrierApplicationMapper;
import com.zhonghui.wms.mapper.WmsMaterialMapper;
import com.zhonghui.wms.mapper.WmsWarehouseAreaMapper;
import com.zhonghui.wms.mapper.WmsWarehouseMapper;
import com.zhonghui.wms.service.IWmsCarrierApplicationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 智能仓储WMS-运输管理-承运申请Service业务层处理
 *
 * @author zhonghui
 * @date 2023-08-08
 */
@Service
public class WmsCarrierApplicationServiceImpl implements IWmsCarrierApplicationService
{
    @Autowired
    private WmsCarrierApplicationMapper wmsCarrierApplicationMapper;

    @Autowired
    private WmsMaterialMapper wmsMaterialMapper;

    @Autowired
    private WmsWarehouseMapper wmsWarehouseMapper;

    @Autowired
    private WmsWarehouseAreaMapper wmsWarehouseAreaMapper;


    /**
     * 查询智能仓储WMS-运输管理-承运申请
     *
     * @param id 智能仓储WMS-运输管理-承运申请主键
     * @return 智能仓储WMS-运输管理-承运申请
     */
    @Override
    public WmsCarrierApplicationVo selectWmsCarrierApplicationById(Long id)
    {
        WmsCarrierApplicationVo wmsCarrierApplicationVo = wmsCarrierApplicationMapper.selectWmsCarrierApplicationById(id);
        for(WmsCarrierApplicationDetails details:wmsCarrierApplicationVo.getWmsCarrierApplicationDetailsList()){
            // 遍历指定仓库中的库区和库位列表，用于前端查询是数据的回显
            details.setWmsWarehouseAreaList(wmsWarehouseMapper.selectWmsWarehouseById(details.getWarehouseId()).getWmsWarehouseAreaList());
            details.setWmsWarehouseSeatList(wmsWarehouseAreaMapper.selectWmsWarehouseAreaById(details.getWarehouseAreaId()).getWmsWarehouseSeatList());
            // 获取物料信息
            details.setMaterialName(wmsMaterialMapper.selectWmsMaterialById(details.getMaterialId()).getMaterialName());
            details.setPrice(wmsMaterialMapper.selectWmsMaterialById(details.getMaterialId()).getMaterialPrice());
        }
        return wmsCarrierApplicationVo;
    }

    /**
     * 查询智能仓储WMS-运输管理-承运申请列表
     *
     * @param wmsCarrierApplication 智能仓储WMS-运输管理-承运申请
     * @return 智能仓储WMS-运输管理-承运申请
     */
    @Override
    public List<WmsCarrierApplicationVo> selectWmsCarrierApplicationList(WmsCarrierApplication wmsCarrierApplication)
    {
        return wmsCarrierApplicationMapper.selectWmsCarrierApplicationList(wmsCarrierApplication);
    }

    /**
     * 新增智能仓储WMS-运输管理-承运申请
     *
     * @param wmsCarrierApplication 智能仓储WMS-运输管理-承运申请
     * @return 结果
     */
    @Transactional
    @Override
    public int insertWmsCarrierApplication(WmsCarrierApplication wmsCarrierApplication)
    {
        String documentNo = generateNumber("CYSQ");
        wmsCarrierApplication.setDocumentNo(documentNo);
        wmsCarrierApplication.setApplicant(SecurityUtils.getLoginUser().getUserId());
        if (wmsCarrierApplication.getApplicationStatus() == null){
            wmsCarrierApplication.setApplicationStatus(Long.parseLong("0"));
        }
        int rows = wmsCarrierApplicationMapper.insertWmsCarrierApplication(wmsCarrierApplication);
        insertWmsCarrierApplicationDetails(wmsCarrierApplication);
        return rows;
    }

    private String generateNumber(String number){
        Date date = new Date();
        SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmmssSSS");
        return number+format.format(date);
    }

    /**
     * 修改智能仓储WMS-运输管理-承运申请
     *
     * @param wmsCarrierApplication 智能仓储WMS-运输管理-承运申请
     * @return 结果
     */
    @Transactional
    @Override
    public int updateWmsCarrierApplication(WmsCarrierApplication wmsCarrierApplication)
    {
        if (wmsCarrierApplication.getApprovedStatus() == null){
            wmsCarrierApplicationMapper.deleteWmsCarrierApplicationDetailsByCarrierApplicationId(wmsCarrierApplication.getId());
            insertWmsCarrierApplicationDetails(wmsCarrierApplication);
        }
        return wmsCarrierApplicationMapper.updateWmsCarrierApplication(wmsCarrierApplication);
    }

    /**
     * 批量删除智能仓储WMS-运输管理-承运申请
     *
     * @param ids 需要删除的智能仓储WMS-运输管理-承运申请主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteWmsCarrierApplicationByIds(Long[] ids)
    {
        wmsCarrierApplicationMapper.deleteWmsCarrierApplicationDetailsByCarrierApplicationIds(ids);
        return wmsCarrierApplicationMapper.deleteWmsCarrierApplicationByIds(ids);
    }

    /**
     * 删除智能仓储WMS-运输管理-承运申请信息
     *
     * @param id 智能仓储WMS-运输管理-承运申请主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteWmsCarrierApplicationById(Long id)
    {
        wmsCarrierApplicationMapper.deleteWmsCarrierApplicationDetailsByCarrierApplicationId(id);
        return wmsCarrierApplicationMapper.deleteWmsCarrierApplicationById(id);
    }

    /**
     * 新增智能仓储WMS-运输管理-承运申请明细信息
     *
     * @param wmsCarrierApplication 智能仓储WMS-运输管理-承运申请对象
     */
    public void insertWmsCarrierApplicationDetails(WmsCarrierApplication wmsCarrierApplication)
    {
        List<WmsCarrierApplicationDetails> wmsCarrierApplicationDetailsList = wmsCarrierApplication.getWmsCarrierApplicationDetailsList();
        Long id = wmsCarrierApplication.getId();
        if (StringUtils.isNotNull(wmsCarrierApplicationDetailsList))
        {
            List<WmsCarrierApplicationDetails> list = new ArrayList<WmsCarrierApplicationDetails>();
            for (WmsCarrierApplicationDetails wmsCarrierApplicationDetails : wmsCarrierApplicationDetailsList)
            {
                wmsCarrierApplicationDetails.setCarrierApplicationId(id);
                list.add(wmsCarrierApplicationDetails);
            }
            if (list.size() > 0)
            {
                wmsCarrierApplicationMapper.batchWmsCarrierApplicationDetails(list);
            }
        }
    }
}
